<template>
  <div class="avatar-unit" @mouseleave="closeToolbar">
    <div class="login" v-if="token">
        <el-avatar 
            class="default-avatar" 
            :src="avatarUrl"
            :style="moveAvatar" 
            @mouseenter="activeToolbar"
        >
        </el-avatar>
        <div class="toolbar" v-show="displayToolbar" @mouseleave="closeToolbar">
            <el-avatar class="active-avatar" :src="avatarUrl" size="large"></el-avatar>
            <p class="nickname">{{userInfo.nickname}}</p>
            <div class="vip" :style="vipStyle">{{userInfo.authority}}</div>
            <div class="line"></div>
            <el-menu-item index="/profile">个人中心</el-menu-item>
            <el-menu-item index="/edit">文章管理</el-menu-item>
            <!-- <el-menu-item index="6-3">历史记录</el-menu-item> -->
            <el-menu-item index="/home" @click="quit">退出登录</el-menu-item>
        </div>
    </div>
    <div class="logout" v-if="!token">
        <el-avatar 
            class="default-avatar"
            title="点击登录"
            :src="require('@/assets/Avatar/logout.png')"
            @click="login"
        >
        </el-avatar>
    </div>
  </div>
</template>

<script>
import { ref } from '@vue/reactivity'
import router from '@/router'
import { onBeforeMount } from '@vue/runtime-core'
import axios from 'axios'
import { ElMessage } from 'element-plus'

export default {
    name: 'AvatarUnit',
    setup() {
        let token = ref(null)//登录状态
        let userInfo = ref({})//用户信息
        let avatarUrl = ref('')//用户头像地址
        let vipStyle = ref('')//权限样式
        let displayToolbar = ref(false)
        let moveAvatar = ref('')

        const activeToolbar = () => {
            moveAvatar.value = 'margin-top: 50px'
            displayToolbar.value = true
        }
        const closeToolbar = () => {
            moveAvatar.value = ''
            displayToolbar.value = false
        }
        const login = () => {
            router.push('/login')
        }
        const quit = () => {
            //移除本地token，服务端如何处理暂时不明
            token.value = null
            userInfo.value = {}
            sessionStorage.removeItem('token')
        }

        //检查登录状态
        onBeforeMount(() => {
           token.value = sessionStorage.getItem('token')//找token
           if(token.value) {
                axios.get('http://localhost/api/getUserInfo', {
                    headers: {
                        Authorization: 'Bearer '+token.value//鉴权请求头
                    }
                }).then(res => {
                    userInfo.value = res.data
                    avatarUrl.value = userInfo.value.avatar ? userInfo.value.avatar : require('@/assets/Avatar/chino.jpg')
                    if(userInfo.value.authority == 'root') {
                        userInfo.value.authority = '阿瓦隆･勒･菲'
                        vipStyle.value = 'background-color: rgb(64,48,119)'
                    } else {
                        userInfo.value.authority = '无名妖精'
                        vipStyle.value = 'background-color: gray'
                    }
                    //console.log(userInfo.value)
                }).catch(err => {
                    console.log('请求失败！')
                    console.log(err)
                    if(err.response.status == 401) {
                        ElMessage.error('登录过期，请重新登录！')
                        quit()
                    }
                })
           }
        })

        return {
            token,
            userInfo,
            avatarUrl,
            vipStyle,
            moveAvatar,
            displayToolbar,
            activeToolbar,
            closeToolbar,
            login,
            quit
        }
    }
}
</script>

<style scoped>
    .avatar-unit {
        position: relative;
        width: 100px;
        margin: 0 25px;
        cursor: pointer;
    }
    .login, .logout {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100%;
        height: 100%;
    }
    .default-avatar {
        position: absolute;
        transition: 0.2s ease-out;
    }
    .active-avatar {
        position: absolute;
        top: -30px;
        left: 50%;
        transform: translateX(-50%);
    }
    .toolbar {
        position: absolute;
        top: 110%;
        width: 120px;
        background-color: #545c64;
        z-index: 100;
        text-align: center;
    }
    .nickname {
        margin-top: 30px;
        margin-bottom: 5px;
        padding: 0 5px;
        color: #48DBFB;
        font-weight: 600;
        overflow: hidden;
        white-space:nowrap;
        text-overflow: ellipsis;
    }
    .vip {
        width: fit-content;
        margin: 0px auto;
        padding: 2px 5px;
        font-size: 12px;
        color: #ffff;
        /* background-color: rgb(64,48,119); */
        background-color: gray;
        border-radius: 4px;
    }
    .line {
        height: 0.5px;
        background-color: #ffff;
        margin: 5px 0;
    }
    .el-menu-item {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 40px;
    }
</style>